package com.zoshine.zzg.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.annotation.DisableLoginCheck;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.*;
import com.zoshine.zzg.pojo.pojostatus.OrgType;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 对外接口控制层
 * @author 贾俊峰
 * @time 2017/8/5 14:42
 */
@RestController
@RequestMapping(value = "api",method = RequestMethod.POST)
@DisableLoginCheck
public class ApiController extends BaseController {
    private FamilyMemberGoodsService familyMemberGoodsService;
    private FamilyMemberOrderService familyMemberOrderService;
    private FamilyMemberOrgService familyMemberOrgService;
    private GoodsTypeService goodsTypeService;
    private PrisonerBasicService prisonerBasicService;
    private PrisonerAccountService prisonerAccountService;
    private FamilyMemberRechargeService familyMemberRechargeService;

    @Autowired
    public void setFamilyMemberRechargeService(FamilyMemberRechargeService familyMemberRechargeService) {
        this.familyMemberRechargeService = familyMemberRechargeService;
    }
    @Autowired
    public void setFamilyMemberGoodsService(FamilyMemberGoodsService familyMemberGoodsService) {
        this.familyMemberGoodsService = familyMemberGoodsService;
    }
    @Autowired
    public void setFamilyMemberOrderService(FamilyMemberOrderService familyMemberOrderService) {
        this.familyMemberOrderService = familyMemberOrderService;
    }
    @Autowired
    public void setGoodsTypeService(GoodsTypeService goodsTypeService) {
        this.goodsTypeService = goodsTypeService;
    }
    @Autowired
    public void setFamilyMemberOrgService(FamilyMemberOrgService familyMemberOrgService) {
        this.familyMemberOrgService = familyMemberOrgService;
    }
    @Autowired
    public void setPrisonerBasicService(PrisonerBasicService prisonerBasicService) {
        this.prisonerBasicService = prisonerBasicService;
    }
    @Autowired
    public void setPrisonerAccountService(PrisonerAccountService prisonerAccountService) {
        this.prisonerAccountService = prisonerAccountService;
    }

    /**
     * 获取监所列表
     * @return 监所集合
     */
    @RequestMapping("org/listPrison")
    public BaseResponse listPrison() {
        Organization organization=new Organization();
        organization.setOrgType(OrgType.Prison.getValue());
        List<FamilyMemberOrg> familyMemberOrgs = familyMemberOrgService.listOrg(organization);
        return new AppResponse(familyMemberOrgs);
    }

    /**
     * 获取供应商列表
     * @return 供应商集合
     */
    @RequestMapping("org/listSupplier")
    public BaseResponse listSupplier() {
        Organization organization=new Organization();
        organization.setOrgType(OrgType.Supplier.getValue());
        List<FamilyMemberOrg> familyMemberOrgs = familyMemberOrgService.listOrg(organization);
        return new AppResponse(familyMemberOrgs);
    }

    /**
     * 家属点单商品列表(分页)
     * @param familyMemberGoods 商品信息
     * @return 商品集合
     */
    @RequestMapping("goods/pageGoods")
    public BaseResponse pageGoods(FamilyMemberGoods familyMemberGoods) {
        if (familyMemberGoods.getPrisonId()==null){
            throw new AppException(ErrorMessage.ACCOUNT_PRISONS_ID_CAN_NOT_NULL);
        }
        if (familyMemberGoods.getSupplierId()==null){
            throw new AppException(ErrorMessage.ACCOUNT_SUPPLIER_ID_CAN_NOT_NULL);
        }
        List<FamilyMemberGoods> list = familyMemberGoodsService.list(familyMemberGoods);
        return new AppResponse(new PageInfo<>(list));
    }

    /**
     *家属点单商品列表(不分页)
     * @param familyMemberGoods 商品信息
     * @return 商品集合
     */
    @RequestMapping("goods/listGoods")
    public BaseResponse listGoods(FamilyMemberGoods familyMemberGoods) {
        if (familyMemberGoods.getPrisonId()==null){
            throw new AppException(ErrorMessage.ACCOUNT_PRISONS_ID_CAN_NOT_NULL);
        }
        if (familyMemberGoods.getSupplierId()==null){
            throw new AppException(ErrorMessage.ACCOUNT_SUPPLIER_ID_CAN_NOT_NULL);
        }
        List<FamilyMemberGoods> list = familyMemberGoodsService.list(familyMemberGoods);
        return new AppResponse(list);
    }

    /**
     * 根据商品ID获取商品信息
     * @param id 商品ID
     * @return 商品信息
     */
    @RequestMapping("goods/getGoods")
    public BaseResponse getGoods(Long id) {
        if (id == null){
            throw new AppException(ErrorMessage.GOODS_ID_CAN_NOT_BE_NULL_OR_UNDER_ZERO);
        }
        FamilyMemberGoods familyMemberGoods = familyMemberGoodsService.get(id);
        return new AppResponse(familyMemberGoods);
    }

    /**
     * 商品类型列表查询
     * @return 商品类型集合
     */
    @RequestMapping("goods/listGoodsType")
    public BaseResponse listGoodsType() {
        List<GoodsType> goodsTypeList = goodsTypeService.list(null);
        return new AppResponse(goodsTypeList);
    }

    /**
     * 新增家属点单信息
     * @param familyMemberOrder 家属点单
     * @return 家属订单信息
     */
    @RequestMapping("order/addOrder")
    public BaseResponse addOrder(String familyMemberOrder) {
        FamilyMemberOrder temp = JSON.parseObject(familyMemberOrder, FamilyMemberOrder.class);
        FamilyMemberOrder backOrder = familyMemberOrderService.addFamilyMemberOrder(temp);
        return new AppResponse(backOrder);
    }

    /**
     * 撤回家属点单信息
     * @param id 家属点单ID
     */
    @RequestMapping("order/cancelOrder")
    public BaseResponse cancelOrder(Long id) {
        familyMemberOrderService.cancelOrder(id);
        return AppResponse.OK;
    }

    /**
     * 家属点单支付成功回调
     * @param id 家属点单ID
     * @param payType 支付类型
     */
    @RequestMapping("order/payOrder")
    public BaseResponse cancelOrder(Long id, Integer payType) {
        familyMemberOrderService.payOrder(id, payType);
        return AppResponse.OK;
    }

    /**
     * 查询家属点单信息集合(分页)
     * @param familyMemberOrder 家属点单信息
     *@return 家属点单信息集合
     */
    @RequestMapping("order/pageOrder")
    public BaseResponse pageOrder(FamilyMemberOrder familyMemberOrder) {
        List<FamilyMemberOrder> familyMemberOrderList = familyMemberOrderService.list(familyMemberOrder);
        return new AppResponse(new PageInfo<>(familyMemberOrderList));
    }

    /**
     * 查询家属点单信息集合(不分页)
     * @param familyMemberOrder 家属点单信息
     * @return 家属点单信息集合
     */
    @RequestMapping("order/listOrder")
    public BaseResponse listOrder(FamilyMemberOrder familyMemberOrder) {
        List<FamilyMemberOrder> familyMemberOrderList = familyMemberOrderService.list(familyMemberOrder);
        return new AppResponse(familyMemberOrderList);
    }

    /**
     * 查询家属点单信息对象
     * @param id 家属点单ID
     * @return 家属点单信息对象
     */
    @RequestMapping("order/getOrder")
    public BaseResponse getOrder(Long id) {
        if (id == null){
            throw new AppException(ErrorMessage.ORDER_PRISONER_ID_CAN_NOT_NULL);
        }
        FamilyMemberOrder familyMemberOrder = familyMemberOrderService.get(id);
        return new AppResponse(familyMemberOrder);
    }

    /**
     * 家属点单详情查看
     * @param orderId 家属点单ID
     * @return 家属点单详情集合
     */
    @RequestMapping("order/listOrderDetail")
    public BaseResponse listOrderDetail(Long orderId) {
        List<FamilyMemberOrderDetail> familyMemberOrderDetailList = familyMemberOrderService.listOrderDetail(orderId);
        return new AppResponse(familyMemberOrderDetailList);
    }

    /**
     * 根据银行卡号获取犯人信息
     * @param bankCard 银行卡号
     * @return 犯人信息
     */
    @RequestMapping("prisonerBasic/getPrisonerByBankCard")
    public BaseResponse getPrisonerByBankCard(String bankCard){
        if (bankCard == null){
            throw new AppException(ErrorMessage.BANK_CARD_NOT_NULL);
        }
        PrisonerBasic prisonerBasic=new PrisonerBasic();
        prisonerBasic.setBankCard(bankCard);
        PrisonerBasic prisonerBasicByBankCard = prisonerBasicService.getPrisonerBasicByBankCard(bankCard);
        return new AppResponse(prisonerBasicByBankCard);
    }

    /**
     * 一体机 根据银行卡号获取犯人信息
     * @param bankCard 银行卡号
     * @return 犯人信息
     */
    @RequestMapping("prisonerBasic/getPrisonerByBankCardInBank")
    public BaseResponse getPrisonerByBankCardInBank(String bankCard){
        if (bankCard == null){
            throw new AppException(ErrorMessage.BANK_CARD_NOT_NULL);
        }
        PrisonerAccount prisonerAccount = prisonerAccountService.getPrisonerByBankCardInBank(bankCard);
        if(null == prisonerAccount){
            throw new AppException(ErrorMessage.PRISONER_BANKCARD_NOT_NULL);
        }
        PrisonerBasic prisonerBasic = new PrisonerBasic();
        prisonerBasic.setBankCard(prisonerAccount.getBankCard());
        prisonerBasic.setName(prisonerAccount.getPrisonerName());
        prisonerBasic.setId(prisonerAccount.getId());
        return new AppResponse(prisonerBasic);
    }

    /**
     * 一体机 家属充值记录
     * @param familyMemberRecharge 家属充值信息
     * @return
     */
    @RequestMapping("Recharge/familyMemberRecharge")
    public BaseResponse familyMemberRecharge(FamilyMemberRecharge familyMemberRecharge){
        familyMemberRechargeService.add(familyMemberRecharge);
        return AppResponse.OK;
    }



}
